function getBrowserLanguage(){
    var lang=navigator.userLanguage?navigator.userLanguage:navigator.language;
    lang=lang.substring(0,2).toLowerCase();
    return lang=="zh"?"zh-CN":"en";
}
//var userBrowserLanguage=getBrowserLanguage();
require.config({
    //urlArgs: "r=" + (new Date()).getTime(),
    waitSeconds: 0,
    map: {"*": {"css": "require/css"}},
    "shim": {
        "underscore": {exports: "_"},
        "jquery": {exports: "$"},
        "bootstrap": ["jquery"],
        "backbone": {deps: ["underscore", "jquery"],exports: "Backbone"},
        "DatePicker":{deps: ["css!../libs/bootstrap-datepicker/css/bootstrap-datepicker3.min.css"]},
        "DateRange":{deps: ["css!../libs/bootstrap-daterangepicker/css/daterangepicker.min.css"]},
        "datatables.net":{deps: ["css!../libs/jquery.dataTables/css/jquery.dataTables.min.css"]},
        "Select2":{deps: ["css!../libs/jquery.select2/css/select2.min.css"]},
        "jsTree":{deps: ["css!../libs/jstree/css/style.min.css"]},
        "XEditable":{deps: ["css!../libs/bootstrap-editable/css/bootstrap-editable.min.css"]},
        "IonRangeSlider":{deps: ["css!../libs/ion.rangeSlider/css/ion.rangeSlider.all.min.css"]},
        "Switchery":{deps: ["css!../libs/switchery/css/switchery.min.css"]},
        "PowerFloat":{deps: ["css!../libs/jquery.powerFloat/css/powerFloat.min.css"]},
        "ColorBox":{deps: ["css!../libs/jquery.colorbox/css/colorbox.min.css"]},
        "SweetAlert":{deps: ["css!../libs/sweetAlert/sweetalert.min.css"]}
    },
    "paths" : {
        "servers":"servers",
        "underscore":"../libs/underscore/underscore.1.8.3.min",
        "backbone": 'backbone/backbone.1.1.2.min',
        "text": 'require/text',
        "jquery":"../libs/jquery/jquery-1.11.3.min",
        //"jquery":"../libs/jquery/jquery-3.1.0",
        "bootstrap": "../libs/bootstrap/js/bootstrap.min",
        "moment": "../libs/moment/moment.min",
        "MomentLang": "../libs/moment/locale/zh-cn",
        "DatePicker": "../libs/bootstrap-datepicker/js/bootstrap-datepicker.min",
        "DateRange": "../libs/bootstrap-daterangepicker/js/daterangepicker",
        "Notify": "../libs/jquery.notify/notify.min",
        "datatables.net": "../libs/jquery.dataTables/js/jquery.dataTables",
        "DataTablesBootstrap": "../libs/jquery.dataTables/js/dataTables.bootstrap.min",
        "CounterUp": "../libs/jquery.counterup/jquery.counterup.min",
        "echarts": "../libs/echart/echarts.min",
        "eChartChina": "../libs/echart/map/china",
        "eChartTheme": "../libs/echart/theme/macarons",
        "Sparkline": "../libs/jquery.sparkline/jquery.sparkline.min",
        "Waves": "../libs/waves/waves.min",
        "Select2": "../libs/jquery.select2/js/select2.min",
        "jsRender": "../libs/jquery.jsrender/jsrender.min",
        "Parsley": "../libs/jquery.parsley/js/parsley.min",
        "XEditable": "../libs/bootstrap-editable/js/bootstrap-editable.min",
        "jsTree": "../libs/jstree/js/jstree.min",
        "IonRangeSlider": "../libs/ion.rangeSlider/js/ion.rangeSlider.min",
        "Switchery": "../libs/switchery/js/switchery.min",
        "AutoNumeric": "../libs/autoNumeric/autoNumeric",
        "FileInput": "../libs/uploadfileinput/elements.fileinput",
        "PowerFloat": "../libs/jquery.powerFloat/js/jquery.powerFloat",
        "ColorBox": "../libs/jquery.colorbox/js/jquery.colorbox",
        "SweetAlert": "../libs/sweetAlert/sweetalert.min",
        "DotDotDot": "../libs/jquery.ellipsis/jquery.dotdotdot",
        "SlimScroll": "../libs/jquery.slimScroll/jquery.slimscroll",
        "Nestable": "../libs/jquery.nestable/jquery.nestable",
        "Particles":"../libs/particles/particles",
        "Cookie":"../libs/jquery.cookie/jquery.cookie",

        "ExtCommon": "../libs/jquery.cl.extend/ext.common",
        "ExtAjax": "../libs/jquery.cl.extend/ext.ajax",
        "ExtNotify": "../libs/jquery.cl.extend/ext.notify",
        "ExtSelect2Tree": "../libs/jquery.cl.extend/ext.select2tree",
        "ExtParsley": "../libs/jquery.cl.extend/ext.parsley",
        "ExtSelect2": "../libs/jquery.cl.extend/ext.select2",
        "ExtDataTable": "../libs/jquery.cl.extend/ext.datatable",
        "ExtMoment": "../libs/jquery.cl.extend/ext.moment",
        "ExtDateTime": "../libs/jquery.cl.extend/ext.datetime",
        "ExtDaterange": "../libs/jquery.cl.extend/ext.daterange",
        "ExtSwitchery": "../libs/jquery.cl.extend/ext.switchery",
        "ExtUploadFile": "../libs/jquery.cl.extend/ext.uploadfile",
        "ExtTabsVelocity": "../libs/jquery.cl.extend/ext.tabsvelocity",
        "ExtNumeric": "../libs/jquery.cl.extend/ext.numeric",
        "ExtXeditable": "../libs/jquery.cl.extend/ext.xeditable",
        "ExtFormValue": "../libs/jquery.cl.extend/ext.formvalue",
        "ExtPowerfloat": "../libs/jquery.cl.extend/ext.powerfloat",
        "ExtSweetAlert": "../libs/jquery.cl.extend/ext.sweetAlert",
        "ExtColorBox": "../libs/jquery.cl.extend/ext.colorbox",
        "ExtRadioCheckbox": "../libs/jquery.cl.extend/ext.radiocheckbox",
        "ExtSelectedMove": "../libs/jquery.cl.extend/ext.selectedmove",
        "ExtWeekHour": "../libs/jquery.cl.extend/ext.weekhour",
        "ExtFullScreen": "../libs/jquery.cl.extend/ext.fullscreen",
        "ExtNestable": "../libs/jquery.cl.extend/ext.nestable",
        "ExtECharts": "../libs/jquery.cl.extend/ext.echarts",
        "ExtReport": "custom/report"
    }
});
// Ladda
var reqModalCommon={
    libsCss:{
        "BaseEnd":["css!../libs/animate/animate.min.css"],
        "AllEnd":["css!../css/pageExtLibs.css","css!../css/pageParticular.css","css!../css/pageThemeBlue.css"]
    },
    libs:{
        Base:["jquery", "underscore","backbone", "router"],
        BaseStart:["bootstrap","Waves","ExtCommon","ExtAjax","ExtNotify","Cookie","assist"],
        Moment:["ExtMoment"], //时间操作 moment
        DateTime:["ExtMoment","ExtDateTime"],//时间
        DateRange:["ExtMoment","ExtDaterange"],//时间段
        DataTables:["ExtDataTable"],// 表格 "datatables.net"
        CounterUp:["CounterUp"],    //数字 跳动
        //EChart:["ExtECharts"], //图表  "echarts","eChartChina","eChartTheme",
        Sparkline:["Sparkline"], // 小图表
        JsRender:["jsRender"],
        IonRangeSlider:["IonRangeSlider"],
        Select2:["ExtSelect2"], //下拉框 "Select2"
        JsTree:["jsTree"], // 树结构
        Select2Tree:["ExtSelect2","ExtSelect2Tree"], // 下拉和树状 "Select2","ExtSelect2","jsTree",
        Parsley:["ExtParsley","ExtFormValue"], //表单验证 Parsley
        XEditable:["ExtXeditable"], //快捷编辑  XEditable
        Switchery:["ExtSwitchery"], // 开关  Switchery
        //Tabs:["ExtTabsVelocity"], Velocity
        Nestable:["Nestable"],//拖动
        PowerFloat:["ExtPowerfloat"], // 浮层HTML PowerFloat
        ColorBox:["ExtColorBox"], //图片预览
        SweetAlert:["ExtSweetAlert"], //对话框  SweetAlert
        Numeric:["ExtNumeric"], //千分位  AutoNumeric
        UploadFile:["ExtUploadFile"], //上传 FileInput
        Targe:["ExtTarge"], // 定向
        FormValue:["ExtFormValue"], //表单赋值
        DotDotDot:["DotDotDot"],//省略号
        RadioCheckbox:["ExtRadioCheckbox"], //单/多选
        SelectedMove:["ExtSelectedMove","jsTree"],//选中项移动区域
        WeekHour:["ExtWeekHour"],//选择 周/小时 定向
        ExtNestable:["ExtNestable"], //拖动
        Particles:["Particles"], //粒子运动
        ExtReport:["ExtReport"] //报表
    },
    pages:{
        Base:["Base"],
        BaseStart:["BaseStart"],
        BaseEnd:["BaseEnd"]
    },
    getReqByPage:function(page){
        var arrLibs=this.pages[page];
        return this.getReqByLibs(arrLibs)
    },
    getReqByLibs:function(arrLibs){
        var arrTemp=[];
        for(var i=0;i<arrLibs.length;i++){
            if(this.libsCss[arrLibs[i]]){
                arrTemp=arrTemp.concat(this.libsCss[arrLibs[i]])
            }
        }
        for(var j=0;j<arrLibs.length;j++){
            arrTemp=arrTemp.concat(this.libs[arrLibs[j]])
        }
        return arrTemp;
    },
    loadingStart:function(){
        this.timeStart=new Date();
        this.isLoading=true;
        var containerBody=document.getElementById("container_body");
        if(containerBody){
            document.getElementsByTagName("body")[0].className=document.body.className+" overflow-h";
            document.getElementById("container_load").style["display"]="block";
            containerBody.style["opacity"]=0;
        }
    },
    showContainer: function () {
        setTimeout(function(){
            $("#container_load").hide();
            $("#container_body").css({"opacity": 1}).show();
            $("body").removeClass("overflow-h")
        },250)
    },
    loadingEnd:function(){
        var self = this;
        require(reqModalCommon.libsCss.AllEnd, function () {
            if (self.isLoading) {
                self.timeEnd = new Date();
                var timeDiff = self.timeEnd.getTime() - self.timeStart.getTime();
                var timeout = (timeDiff - 100) > 0 ? 0 : Math.abs(timeDiff - 100);
                //console.log(timeDiff + "...100....." + timeout);
                if (timeout > 0) {
                    setTimeout(function () {
                        self.showContainer()
                    }, timeout);
                } else {
                    self.showContainer()
                }
                $("link[href$='css/pageExtLibs.css'],link[href$='css/pageParticular.css'],link[href$='css/pageThemeBlue.css']").appendTo("head");
                self.isLoading = false;
            }
        });
    }
};
//var pageBasePathConfig="/adx";
var pageBasePathConfig="/ltttest";
var controllerCongfig={
    Layout:["Account","Basic.Base"],
    Account:["Account"],
    Report:["Report"],
    Media:["Media","Basic.Base"],
    AdSpace:["AdSpace","Basic.Base","Media","Template"],
    Template:["Template"],
    Dsp:["Dsp"],
    Customer:["Customer"],
    Creative:["Creative"]
};
var viewCongfig={
    layout:{
        index:{view:"Index",controller:"Layout", libs:[]},
        login:{view:"Login",controller:"Layout", libs:["Particles"]}
    },
    account:{
        login:{title:"登录",view:"Login",controller:"Account", libs:["Parsley"]}
    },
    report:{
        overview:{menuId:"reportOverview",title:"账户概要",view:"Overview",controller:"Report", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp"]},
        consume:{menuId:"reportConsume",title:"总体消耗报告",view:"Consume",controller:"Report", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp"]},
        customer:{menuId:"reportCustomer",title:"客户效果报告",view:"Customer",controller:"Report", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp","Select2"]},
        bid:{menuId:"reportBid",title:"竞价明细报告",view:"Bid",controller:"Report", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp"]}

    },
    media:{
        index:{menuId:"media",title:"媒体管理",view:"List",controller:"Media", libs:["DataTables","JsRender","Select2","Parsley","SweetAlert"]},
        overview:{menuId:"mediaOverview",title:"概况报告",view:"Overview",controller:"Media", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp","Select2"]},
        adspace:{menuId:"mediaAdSpace",title:"广告位报告",view:"AdSpaceList",controller:"Media", libs:["DataTables","Numeric","DateRange"]},
        adspacereport:{menuId:"mediaAdSpace",title:"广告位报告详情",view:"AdSpaceReport",controller:"Media", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp"]}
    },
    adspace:{
        index:{menuId:"adspace",title:"广告位管理",view:"List",controller:"AdSpace", libs:["DataTables","Select2","JsRender","XEditable","SweetAlert"]},
        detail:{menuId:"adspace",title:"广告位详情",view:"Detail",controller:"AdSpace", libs:["DataTables","Select2","Parsley","Numeric","RadioCheckbox","Switchery"]}
    },
    template:{
        index:{menuId:"template",title:"原生模板管理",view:"List",controller:"Template", libs:["DataTables","JsRender","SweetAlert","ColorBox"]},
        detail:{menuId:"template",title:"原生模板详情",view:"Detail",controller:"Template", libs:["DataTables","JsRender","SweetAlert","UploadFile","Parsley","Select2","Numeric"]}
    },
    dsp:{
        index:{menuId:"dsp",title:"DSP管理",view:"List",controller:"Dsp", libs:["DataTables","Moment"]},
        detail:{menuId:"dsp",title:"DSP详情",view:"Detail",controller:"Dsp", libs:["Parsley"]},
        consume:{menuId:"consume",title:"DSP消耗报告",view:"ConsumeList",controller:"Dsp", libs:["DataTables","DateRange","Numeric"]},
        consumereport:{menuId:"consume",title:"DSP消耗报告详情",view:"ConsumeReport",controller:"Dsp", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp"]},
        bid:{menuId:"bid",title:"DSP竞价报告",view:"BidList",controller:"Dsp", libs:["DataTables","DateRange","Numeric","Select2"]},
        bidreport:{menuId:"bid",title:"DSP竞价报告详情",view:"BidReport",controller:"Dsp", libs:["DataTables","DateRange","ExtReport","Numeric","CounterUp"]}
    },
    customer:{
        index:{menuId:"customer",title:"客户管理",view:"List",controller:"Customer", libs:["DataTables","Select2","DateRange","JsRender"]},
        detail:{menuId:"customer",title:"客户详情",view:"Detail",controller:"Customer", libs:["Parsley","Select2","RadioCheckbox","UploadFile"]}
    },
    creative:{
        index:{menuId:"creative",title:"创意管理",view:"List",controller:"Creative", libs:["DataTables","Select2","DateRange","JsRender"]},
        detail:{menuId:"creative",title:"创意详情",view:"Detail",controller:"Creative", libs:["Parsley","RadioCheckbox"]}
    }
};
var backboneCommon={
    checkExistPath:function (c,v) {
        var arr=typeof c =="string"?[c]:c;
        var isExist=viewCongfig.hasOwnProperty(arr[0]);
        if(isExist){
            var controller=viewCongfig[arr[0]];
            if(arr.length=2){
                if(isExist){isExist=viewCongfig[arr[0]].hasOwnProperty(arr[1])}
                controller=isExist?viewCongfig[arr[0]][arr[1]]:controller
            }
            isExist=controller.hasOwnProperty(v);
            isExist=isExist?controller[v].hasOwnProperty("controller"):false;
        }
        return isExist
    },
    getMenuId:function(type){
        var info=this._getMvcInfo(type);
        return info.page.menuId;
    },
    setTitleName:function(title){
        if(title){$.ExtAssist.initTilte(title);}
    },
    _getMvcInfo:function(type){
        var pageInfo=this._getConfigView(type);
        var model=[],view=[],controller=[];
        if(pageInfo.controller){
            var arrC=pageInfo.controller.split(".");
            model=controllerCongfig[arrC[0]];
            if(arrC.length==2){model=controllerCongfig[arrC[0]][arrC[1]];}
            controller=[pageInfo.controller];
            if(pageInfo.view){
                var path=pageInfo.controller+"."+pageInfo.view;
                view=[path];
            }
        }
        var template=pageInfo.template?[pageInfo.template]:view;
        return {model:model,view:view,controller:controller,template:template,page:pageInfo}
    },
    _getConfigView:function(type){
        var arrName=type.split(".");
        var info={};
        if(arrName.length>1){
            info=this._getConfigInfoByArr(0,arrName,viewCongfig);
        }else{
            info=viewCongfig[type];
        }
        info=info||viewCongfig["Home"];
        return info;
    },
    _getConfigInfoByArr:function(i,arr,config){
        var info=config[arr[i]];
        if(arr.length==i+1){}
        else{
            i++;
            info=this._getConfigInfoByArr(i,arr,info)
        }
        return info;
    },
    _changeArr:function(value){
        value=value||[];
        value=typeof value=="string"?[value.toString()]:value;
        return value;
    },
    _changeFile:function(file){
      return file.replace(/[.]/g, '/')
    },
    _getModel:function(type){
        return "page/model/"+this._changeFile(type);
    },
    _getView:function(type){
        return "page/view/"+this._changeFile(type);
    },
    _getController:function(type){
        return "page/controller/"+this._changeFile(type);
    },
    _getTemplate:function(type){
        return "text!"+pageBasePathConfig+"/view/"+this._changeFile(type)+".html"
    },
    getControllerByArr:function(controller){
        var arr=[];
        if(controller.length>0){
            arr.push(this._getController(controller[0]));
        }
        return arr;
    },
    getViewByArr:function(view){
        var arr=[];
        if(view.length>0){
            arr.push(this._getView(view[0]));
        }
        return arr;
    },
    getRouterDefine:function(type){
        var arr=[];
        var info=this._getMvcInfo(type);
        if(info.controller.length>0){
            arr.push(this._getController(info.controller[0]));
        }
        return arr;
    },
    getControllerDefine:function(type){
        var arr=["require"];
        var arrType=type.split(".");
        var model=controllerCongfig[arrType[0]];
        if(arrType.length==2 && model){model=controllerCongfig[arrType[0]][arrType[1]];}
        if(model && model.length>0){
            for(var m=0;m<model.length;m++){
                arr.push(this._getModel(model[m]))
            }
        }
        return arr;
    },
    getViewDefine:function(type){
        var info=this._getMvcInfo(type);
        var arr=["jquery","require"];
        if(info.controller.length>0){arr.push(this._getController(info.controller[0]));}
        if(info.template.length>0){arr.push(this._getTemplate(info.template[0]));}
        if(info.page.viewExtra && info.page.viewExtra.length>0){
            for(var i=0;i<info.page.viewExtra.length;i++){
                arr.push(this._getView(info.page.viewExtra[i]));
            }
        }
        if(info.page.libs) {
            arr = arr.concat(reqModalCommon.getReqByLibs(info.page.libs));
        }
        return arr;
    },
    getTemplateDefine:function(type){
        var info=this._getMvcInfo(type);
        var arr=["jquery"];
        if(info.template.length>0){arr.push(this._getTemplate(info.template[0]));}
        if(info.page.libs){
            arr=arr.concat(reqModalCommon.getReqByLibs(info.page.libs));
        }
        return arr;
    }
};
reqModalCommon.loadingStart();
require(reqModalCommon.getReqByPage("Base"), function () {
    var pageLib=reqModalCommon.getReqByPage("BaseStart");
    pageLib = pageLib.concat(reqModalCommon.getReqByPage("BaseEnd"));
    require(pageLib, function () {
        Backbone.history.start();
    });
});